package com.defang.award;

import com.defang.application.EnrollResource;
import com.defang.application.TicketResouce;
import com.defang.application.TicketTypeResource;
import com.defang.open_apis.alipay_server.AlipayServer;
import com.defangit.award.customization.teeth.TeethAwardListener;
import com.defangit.award.extend.point.AwardListener;
import com.defangit.award.extend.wxmp.WxmpPluginEntry;
import com.defangit.award.orm.HibernateUtil;
import com.fasterxml.jackson.jaxrs.annotation.JacksonFeatures;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import com.laogen.jackson.JacksonJsonUtil;
import com.laogen.jersey_util.ImageUploadResource;
import com.laogen.jersey_util.providers.CORSFilter;
import com.laogen.jersey_util.providers.ExceptionMapperSupport;
import com.laogen.jersey_util.providers.HibernateSessionFilter;
import com.laogen.jersey_util.providers.SLF4JLoggingFeature;
import com.laogen.plugins.PluginManager;
import laogen.hibernate.SessionFactoryProvider;
import laogen.hibernate.util.config.DefaultMySqlConfig;
import laogen.util.URIUtil;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.slf4j.LoggerFactory;
import org.slf4j.bridge.SLF4JBridgeHandler;

import java.util.HashMap;
import java.util.Map;

public class JsonResourceConfig extends ResourceConfig {//入口
    static{
        SLF4JBridgeHandler.removeHandlersForRootLogger();
        SLF4JBridgeHandler.install();
    }
    public JsonResourceConfig() {
        super(//构造方法
                AwardResource.class,
                PrizeResource.class,
                TicketResouce.class,
                EnrollResource.class,
                TicketTypeResource.class,
                MultiPartFeature.class,
                ImageUploadResource.class,
                WinningInfoResource.class,
                SLF4JLoggingFeature.class,
                CORSFilter.class,
                JacksonFeatures.class,
                JacksonJsonProvider.class,
                JacksonMapperProvider.class,
                ExceptionMapperSupport.class
        );
        JacksonJsonUtil.addPersistProperties(
                "com.defangit.award.domain.Prize",
                "award",
                new String[]{"id","awardName"},
                this.getClass()
        );
        //在ticketType类的enroll属性序列化时终止循环
        JacksonJsonUtil.addPersistProperties(
                "com.defangit.applicant.domain.TicketType",
                "enroll",
                new String[]{"id","enrollName"},
                this.getClass()
        );
//        JacksonJsonUtil.addPersistProperties(
//                "com.defangit.applicant.domain.Enroll",
//                "ticketTypes",
//                new String[]{"id","ticketName","allowedTickets","maxLength"},
//                this.getClass()
//        );
        registerInstances(new HibernateSessionFilter(SessionFactoryProvider.threadLocalSession));

        //1.只用一次   jsonResource.config
        //支付ID,公钥，秘钥，用来和告诉支付宝/微信，是谁
        AlipayServer.configWap("2017051607251371",
                "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCR6iRQmRuCyBED\n" +
                        "6vzjGfGtA/xhJWhOUgZuy2J4gsu9P3aBcO3J0mTK4NX0umkJNe58cX5vlN+VNmVq\n" +
                        "fGsY1mjmKtuI+iu7sQo09Kt37ljB7qY7HKqVdCQefD9leqzZLMqu76HjAYLfmc7Q\n" +
                        "QoViuOLWzapMowNn72iMdEJbY8holVg4HIplaFB4bJ6jGS7CGI/C3MDPpCFBVP+X\n" +
                        "r005jre/unZi5eSCDUo3WZndO2wQcq8UGcIgaDjWJ7nfh+/O4AxhHPc9/bjuFRkB\n" +
                        "pautZo50C5YApg/2EymvU+GcZ/cZaptgnWv2xiOaxXpBQmiBT1sU+r3JfXLtyete\n" +
                        "K9S9XT4PAgMBAAECggEAfLpOAotuRdPpH5U+SI7D1KaElTIZwGFDWHAIo4UCzamz\n" +
                        "DiDPUubC+91HZyP1Z0cvrjaB4rW6jVzrQiRKk/erwZw+/w8CVTWXyP6gmCsoudXb\n" +
                        "SMpPUuPtgBWrR36exCISu4jhtB91lNdwPvut816hoQY7Hbl2zuCKWQ2EL4TTKfm8\n" +
                        "7t/4WnAKRMvQAzrYVX7kESSeTmWs/dHc8R2Yf6I4J1KMsczV/+pdBAQBpHVb47Mj\n" +
                        "4x77M8yoJFpr8lOXq4JewLsPi9PRDcdBq20faYtRzJIynLHMjJMHUQTdJR/LNfUb\n" +
                        "DzmTbJFRgLT0gKFvgOuTI3f3thjYJQlTGNxbt4EIeQKBgQDEuhZASnD3D/JJiWqO\n" +
                        "mZgz84fn1+yxYnop+L6TSj4Om8tl+FfKUKy44C0guW7LBCeTb40O5ws8v8poBHQ6\n" +
                        "LpHnE0JewoGRTxBazLZKCaMEkBL++/PQmyMgYAHsnOuFBdKY6GiwzdkJRUJc5e9z\n" +
                        "nV9/GnByxWDTZnZ50Y/xqQ9zuwKBgQC94M78WYrGL7U6T/1BDmqb2Yck8r6qHSUw\n" +
                        "ewicAbf283SBGWdWnWKls5GVkzaq8PGJ9FnkfJIW8ravCI1lfJQ8HHFCA8U/dpWS\n" +
                        "0HBZQFB4vanEHN9tWQLa3hv0uU8tLhnsB97O79svSA+4IocAHwE10a6RDRySYKVP\n" +
                        "M9Jlgj0XvQKBgA/7PguDAKJRfVFUBIAJIkA2LHg9JlFSurHmLzUv9tKg/WkPP7KD\n" +
                        "00RBkejyp3l+oOhtS3xGcLdCrWK92eNQHTI0clPYYUY4sg9uSfDBz6hzO9fTB/+O\n" +
                        "WtAqfH2IUenAi1c4qiiS0GrcYYPCei1eP3/H2hK4ziDeVgmXaKYtIcA5AoGAPaIw\n" +
                        "bdc+5Y/s2q3GPgzI7CfS51poK13kiXUWWVVxepaR9aniUkoqEDFWi1/KufgV3zrS\n" +
                        "xwjsfshwJia3hv7GOzDcTppct3ntmUdRgJcQx/CAn6jjmWHwPE0mc+yG19qEDYqM\n" +
                        "d02CLznL6HBEs/RcdzTF3k2MPbNjVW8z10FMRV0CgYAgmk+b929MU/AcxZW5MwW1\n" +
                        "Ed7wGX9goK1JI72iOofSxbnWnAZ01wLrOdx5dEhrZkQJx6lntj6oS9HRn30hfCuN\n" +
                        "CVdoOix4UCsyGReR0qY62WN6fkxcWoXzH3ZvDPlobYLwM1iWRzpoARLzK1gbREck\n" +
                        "zl2SFVWDHl6vM/okHLVjJQ==",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxv3xVeDV/N7pOnev+LyjzV3uZ/eNkU6AyPqD+XhXNirPqB3mbuIBtLKbDscyHM7rQJTN/keuAvduJi0fSHHyd41ZuTdjS57kVZJET9GqRsEN+RN/NKvCNGtCUWl2jmPC4yx0KcKAXutsDofqw6MsWsmaXMdNk6xYXjElYskOABedKuwnAQ0GsCnneT05xwWob8CXjE2Ngk3NEOrx8wmO3kXTLXvJL+66wItD6ikkHp55aPlYl62uXveyevjTvn7xGSfDSjgbl69bEb0kljDvbYr2WcGygfmojSwNNXgA9clbHZX9fwt0kIafWb0X1/XZKLMofYB1YD/O1RgIFs0zJwIDAQAB");

//        SimpleModule module=new SimpleModule();
//        module.addSerializer(EncashIndustryResponse.class,new EncashIndustryResponseSerializer());
//        WechatMapper.getInstance().registerModule(module);
        LoggerFactory.getLogger(JsonResourceConfig.class).error("started");

        String apiPath = URIUtil.getClassPathFile(JsonResourceConfig.class).getParentFile().getParent();
        System.setProperty("com.laogen.plugins.dir", apiPath + "/plugins");
        System.setProperty("com.defangit.work.dir", apiPath);

        PluginManager.getInstance().registerListener(AwardListener.class,new TeethAwardListener());
//        new WxmpPluginEntry().start();

        PluginManager.getInstance().loadAll();
        PluginManager.getInstance().getRegisteredClasses("rest")
                .parallelStream()
                .forEach(this::register);
        HibernateUtil.INSTANCE.setDataSourceConfig(
                new DefaultMySqlConfig()
                        .setAddress("192.168.1.4:3306/qbctest")//www.zm-it.cn:3306/award
                        .setUserName("root")
                        .setPassword("123456")
                        .setHbm2ddl("update")
                        /*.setAddress("59.110.221.35/award")
                        .setUserName("test-lt")
                        .setPassword("576415")
                        .setHbm2ddl("033670")*/
//                       .setAddress("192.168.0.166:3306/award")
//                        .setUserName("root")
//                        .setPassword("root")
//                        .setHbm2ddl("update")
        );
       // module.addSerializer(new EncashIndustryResponse());
    }
}
